package com.top.ruizhong.main.video

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Matrix
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.module.lemlin.base.loadCropImage
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView
import com.top.ruizhong.R
import com.top.ruizhong.entity.VideoList

class CustomGSYVideoPlayer : StandardGSYVideoPlayer {

    private lateinit var ibControlsStart: ImageButton
    private lateinit var ivControlsThumb: ImageView

    //用户头像
    private lateinit var ivControlsHeads: ImageView

    //关注按钮
    private lateinit var ivControlsAttention: ImageView

    //点赞
    private lateinit var tvControlsApprove: TextView

    //评论
    private lateinit var tvControlsDiscuss: TextView

    //分享
    private lateinit var tvControlsShare: TextView

    //用户名称
    private lateinit var tvControlsName: TextView

    //标题内容
    private lateinit var tvControlsTitle: TextView

    private var mVideoList: VideoList? = null

    constructor(context: Context?, fullFlag: Boolean?) : super(
        context, fullFlag
    )

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(
        context, attrs
    )

    override fun getLayoutId(): Int {
        return R.layout.custom_controls_video
    }

    override fun init(context: Context) {
        super.init(context)
        ibControlsStart = findViewById(R.id.ibControlsStart)
        ivControlsThumb = findViewById(R.id.ivControlsThumb)
        ivControlsHeads = findViewById(R.id.ivControlsHeads)
        ivControlsAttention = findViewById(R.id.ivControlsAttention)

        tvControlsApprove = findViewById(R.id.tvControlsApprove)
        tvControlsDiscuss = findViewById(R.id.tvControlsDiscuss)
        tvControlsShare = findViewById(R.id.tvControlsShare)
        tvControlsName = findViewById(R.id.tvControlsName)
        tvControlsTitle = findViewById(R.id.tvControlsTitle)

        ibControlsStart.setOnClickListener(this)
//        ivControlsHeads.setOnClickListener(this)

        if (mThumbImageViewLayout != null &&
            (mCurrentState == -1 || mCurrentState == GSYVideoView.CURRENT_STATE_NORMAL || mCurrentState == GSYVideoView.CURRENT_STATE_ERROR)
        ) {
            mThumbImageViewLayout.visibility = View.VISIBLE
        }

    }

    override fun touchSurfaceMoveFullLogic(
        absDeltaX: Float,
        absDeltaY: Float
    ) {
        super.touchSurfaceMoveFullLogic(absDeltaX, absDeltaY)
        //不给触摸快进，如果需要，屏蔽下方代码即可
        mChangePosition = false
        //不给触摸音量，如果需要，屏蔽下方代码即可
        mChangeVolume = false
        //不给触摸亮度，如果需要，屏蔽下方代码即可
        mBrightness = false
    }

    override fun onClickUiToggle(e: MotionEvent) {
        touchDoubleUp(e)
        if (mCurrentState == 0) {
            startPlayLogic()
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        if (v?.id == R.id.ibControlsStart) {
            onVideoResume(false)
        } else if (v?.id == R.id.ivControlsHeads) {
            ToastUtils.showShort(mVideoList?.title)
        }
    }

    override fun resolveUIState(state: Int) {
        super.resolveUIState(state)
        if (state == CURRENT_STATE_PLAYING) {
            ibControlsStart.visibility = View.GONE
        } else if (state == CURRENT_STATE_PAUSE) {
            ibControlsStart.visibility = View.VISIBLE
        }

        if (state == CURRENT_STATE_PLAYING) {

//            ivControlsThumb.drawable?.let {
//                val m: Matrix = ivControlsThumb.imageMatrix
//                val values = FloatArray(10)
//                m.getValues(values)
//                LogUtils.e("====${ivControlsThumb.drawable?.bounds?.width()?:0 / values[0]} == ${ivControlsThumb.drawable?.bounds?.height()?:0 / values[4]}")
//            }

            GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_FULL)
//            if (currentVideoHeight.toFloat() / currentVideoWidth.toFloat() >= 16f / 9f) {
//                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_FULL)
//            } else {
//                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_DEFAULT)
//            }
        }
    }

    fun setVideoList(videoList: VideoList) {
        this.mVideoList = videoList

        tvControlsApprove.text = quantityUnit(videoList.likes)
        tvControlsDiscuss.text = quantityUnit(videoList.comments)
        tvControlsShare.text = quantityUnit(videoList.shares)
        tvControlsName.text = videoList.title
        tvControlsTitle.text = videoList.description
        ivControlsThumb.loadCropImage(videoList.main_image)
    }

    private fun quantityUnit(quantity: Int): String {
        if (quantity >= 10000) {
            return String.format("%.1fw", quantity.toDouble() / 10000)
        }
        return quantity.toString()
    }


    var mCoverOriginUrl: String? = null
    fun loadCoverImage(url: String) {
//        mCoverOriginUrl = url
//        Glide.with(context.applicationContext)
//            .setDefaultRequestOptions(
//                RequestOptions()
//                    .frame(1000000)
//                    .centerCrop()
//            )
//            .load(url)
//            .into(ivControlsThumb)
    }

    override fun onPrepared() {
        super.onPrepared()
//        LogUtils.e("=====${playPosition}==============${currentVideoWidth}=======${currentVideoHeight}")
    }

}